package com.yc.cloud.wechat.service.impl;

import org.springframework.stereotype.Service;

import com.yc.cloud.basic.exception.Asserts;
import com.yc.cloud.wechat.common.WeChatTips;
import com.yc.cloud.wechat.dto.QRCodeDto;
import com.yc.cloud.wechat.dto.WxAccessTokenDto;
import com.yc.cloud.wechat.dto.WxUserInfoDto;
import com.yc.cloud.wechat.dto.resp.WxServiceAccessTokenErrorResp;
import com.yc.cloud.wechat.service.WxService;
import com.yc.cloud.wechat.utils.WxFuWuHaoUtils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

/**
 * 服务号
 */
@Service("wxFuWuHaoServiceImpl")
@Slf4j
public class WxFuWuHaoServiceImpl implements WxService {

    // public final static String appid = "wxcdc4f4a03133cb4a";
    // public static final String appKey = "eec37c6bbd3a82d59ddee0b44f657e25";

    // @Override
    // public WxAccessTokenVo getAccessToken(String code) {
    // val result = WxServiceAccountUtils.getAccessToken(code);
    // if (StrUtil.isNotEmpty(result)) {
    // if (result.contains("errcode")) {
    // val accessTokenResp = JSONUtil.toBean(result,
    // WxServiceAccessTokenErrorResp.class);
    // Asserts.fail(accessTokenResp.getErrmsg());
    // }
    // return JSONUtil.toBean(result, WxAccessTokenVo.class);
    // }
    // return null;
    // }

    @Override
    public WxAccessTokenDto getAccessToken(String code, String appid, String appKey) {
        val result = WxFuWuHaoUtils.getAccessToken(code, appid, appKey);
        if (StrUtil.isNotEmpty(result)) {
            if (result.contains("errcode")) {
                val accessTokenResp = JSONUtil.toBean(result, WxServiceAccessTokenErrorResp.class);
                Asserts.fail(accessTokenResp.getErrmsg());
            }
            return JSONUtil.toBean(result, WxAccessTokenDto.class);
        }
        return null;
    }

    @Override
    public String getOpenId(String code, String appid, String appKey) {
        return null;
    }

    @Override
    public WxUserInfoDto getUserInfo(String accessToken, String openId) {
        val result = WxFuWuHaoUtils.getUserInfo(accessToken, openId);
        System.out.println(result);
        checkResult(result);
        return JSONUtil.toBean(result, WxUserInfoDto.class);
    }

    @Override
    public byte[] getQRCode(String accessToken, QRCodeDto qrCodeDto) {
        return null;
    }

    public void checkResult(String result) {
        if (StrUtil.isEmpty(result)) {
            Asserts.fail(WeChatTips.REQUEST_RESULT_EMPTY);
        } else {
            if (result.contains("errcode")) {
                val accessTokenResp = JSONUtil.toBean(result, WxServiceAccessTokenErrorResp.class);
                Asserts.fail(accessTokenResp.getErrmsg());
            }
        }
    }

    @Override
    public String getPhoneNumber(String code, String accessToken) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getPhoneNumber'");
    }
}
